<?php
/*
 * Author :pandiam &basanas
 */
require_once ('include/css_head.php');
require_once ('include/functions.php');
require_once ('include/header.php');

require_once ('../BL/BL.php');
require_once ('include/functions.php');
require_once ('../Frameworks/Objects/entity.php');
require_once ('../Frameworks/Objects/attribute.php');
require_once ('../Frameworks/Objects/category.php');

if (!isset($_SESSION['uid']) || $_SESSION['uid'] == -1) {
    UI_registerError('msg_warning', "You are not logged in.");
    header('Location: login.php');
    exit(0);
}

if ( isset($_GET['id']) )
{
    if ( !ctype_digit((string)$_GET['id']) ) {
        UI_registerError('msg_error', "Invalid category request");
        header("location: index.php");
        exit(0);
    }
    $catid = $_GET['id'];
    $entity = $_GET['ent'];
}
else
{
    UI_registerError('msg_warning', "You haven't selected a category to edit entities of!");
    header("location: profile.php");
    exit(0);
}

$cat = BL_getCategory($catid);
$ent = BL_getEntity($entity);
$attributes = $ent->getAttributes();
if ( !empty($attributes) )
    $numAttributes = count($attributes);
else
    $numAttributes = 0;

$i=0;
?>
    <script type="text/javascript" src="js/insertEntityLib.js"></script>

    <div id="msg-box">
        <?php UI_printErrors() ?>
    </div>
    <div id="profileSideBar" style="min-width: 260px; margin-right: 50px; float:left"></div>
    <div id="content">
        <form id="editEntity" name="editEntity" action="editEntityDo.php?id=<?php echo $_GET['id']?>&ent=<?php echo $ent->getId()?>" method="post" style="margin-top: 3%;">
    <?php
        if ( $catid != -1 )
        {
    ?>
            <legend>
                Edit Entity
            </legend>
            <table style="margin-top: 1%">
                <tr>
                    <th><h3>Category: <?php echo $cat->GetName();?></h3></th>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
                <tr align="right">
                    <td>Entity Name :</td>
                    <td>
                        <input style="margin-top: 3px;" type="text" id="entityName" name="entityName" size="30" maxlength="30" value="<?php echo $ent->getName()?>"/>
                    </td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td align="center">
                        <h3 align="center">Attributes</h3>
                    </td>
                </tr>
                <?php
                foreach ($attributes as $attr)
                {
                ?>
                <tr align="right">
                    <td><?php echo $attr->GetName();?> :</td>
                    <td>
                        <input style="margin-top: 3px;" type="text" id="attr<?php echo $i;?>" name="attr<?php echo $i;?>" size="30" maxlength="30" value="<?php echo $attr->getValue() ?>"/>
                    </td>
                </tr>
                <?php
                    $i++;
                }
                ?>
            </table>

            <input id="attrNum" name="attrNum" type="hidden" value="<?php echo $numAttributes;?>" />

            <input class="button" type="submit" value="Edit" />
        <?php
         }
        ?>
       </form>
    </div>
    
    <script type="text/javascript">
        $.ajaxSetup ({ cache: false });
        var ajax_load = "<img src='images/load.gif' alt='loading...' />";
        $("#profileSideBar").load($("#profileSideBar").html(ajax_load).load('profileSideBar.php'));
    </script>
<?php
require_once ('include/footer.php');
?>